using System;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class CharacterBase : MonoBehaviour
{
    [SerializeField] private int maxHp;
    [SerializeField]protected Animator animator;
    protected BoxCollider2D coll;

    public InVariable hp;
    public InVariable defense;
    public InVariable buffRound;

    public bool getPlayer;
    public bool isDead;
    public GameObject buff;
    public GameObject debuff;

    //力量相关
    public float baseStrength;

    public Player player;

    //移动的坐标
    public Vector3 targetPos;

    [Header("事件广播")] 
    public ObjectEventSO moveHealthBarEvent;
    public ObjectEventSO characterDeadEvent;

    public int currentHp
    {
        get => hp.currentValue;
        set => hp.SetValue(value);
    }

    public int MaxHp
    {
        get => hp.maxValue;
    }

    protected virtual void Awake()
    {
        animator = GetComponentInChildren<Animator>();
        coll = GetComponent<BoxCollider2D>();
    }

    protected virtual void Start()
    {
        hp.maxValue = maxHp;
        currentHp = MaxHp;
        buffRound.currentValue = buffRound.maxValue;
        ResetDefense();
        getPlayer = false;
    }

    private void Update()
    {
        if (getPlayer)
        {
            if (player.enemys.Count == 0)
            {
                characterDeadEvent.RaisEvent(this, this);
            }
            if(player.currentHp <= 0)
            {
                characterDeadEvent.RaisEvent(player, this);
            }
        }
       
    }


    public void TakeDamage(int damage)
    {
        //计算当前伤害值
        //计算当前防御值
        var currentDefense = damage - defense.currentValue;

        if (currentDefense > 0)
        {
            //更新防御值
            var moreDamage = damage - defense.currentValue;
            ResetDefense();
            damage = moreDamage;
        }
        else
        {
            var moreDefense = defense.currentValue - damage;
            ResetDefense();
            UpdateDefense(moreDefense);
            damage = 0;
        }

        if (currentHp > damage)
        {
            currentHp -= damage;
            animator.SetTrigger("hit");
            animator.SetBool("isSleep", false);
        }
        else
        {
            currentHp = 0;
            //当前人物死亡
            isDead = true;
            animator.SetBool("isDead", isDead);
            player.enemys.Remove(gameObject);
            coll.enabled = false;
        }

    }

    public void UpdateDefense(int amount)
    {
        var value = defense.currentValue + amount;
        defense.SetValue(value);
    }

    public void ResetDefense()
    {
        defense.SetValue(0);
    }

    public void HealHealth(int amount)
    {
        currentHp += amount;
        //currentHp = currentHp >= MaxHp ? MaxHp : currentHp;
        currentHp = Mathf.Min(currentHp, MaxHp);
        buff.SetActive(true);
    }

    public void SetUpStrength(int round, float buffPercent, bool isPositive)
    {
        if (isPositive)
        {
            float newStrength = baseStrength + buffPercent;
            baseStrength = Mathf.Min(newStrength, 2.0f);
            buff.SetActive(true);
        }
        else
        {
            baseStrength = 1 - buffPercent;
            debuff.SetActive(true);
        }

        //var currentRound = buffRound.currentValue + round;
        if (baseStrength == 1)
        {
            buffRound.SetValue(0);
        }
        else
        {
            //buffRound.SetValue(currentRound);
            buffRound.SetValue(round);
        }

    }

    public void UpdateStrengthRound()
    {
        buffRound.SetValue(buffRound.currentValue - 1);
        if (buffRound.currentValue <= 0)
        {
            buffRound.SetValue(0);
            baseStrength = 1;
        }
    }

    public void GetPlayer(Player Player)
    {
        player = Player;
    }

    public void SetupMana(int value)
    {
        player.AddMana(value);
    }

    public void StartMovePos()
    {
        transform.DOMoveX(targetPos.x, 0.5f).SetEase(Ease.InCirc).onComplete = () =>
        {
            moveHealthBarEvent?.RaisEvent(this, this);
        };
    }

    public void OnAfterLoadRoomEvent(object obj)
    {
        player = obj as Player;
        GetPlayer(player);
        getPlayer = true;
    }
}
